package com.ruibang.glass.purchase.controller;


import com.ruibang.glass.common.domain.camunda.BpmProcessInstanceCreate;
import com.ruibang.glass.purchase.domain.req.PurchaseApplicationReq;
import com.ruibang.glass.purchase.domain.resp.FrameItemData;
import com.ruibang.glass.purchase.domain.resp.OrderItemData;
import com.ruibang.glass.purchase.domain.resp.RequestItemImportData;
import com.ruibang.glass.purchase.entity.PurchaseApplicationList;
import com.ruibang.glass.purchase.entity.PurchaseApprovalList;
import com.ruibang.glass.purchase.service.PurchaseApplicationService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 采购申请列表
 *
 * @author mameng
 */
@Api(tags = "采购申请列表")
@RestController
@RequestMapping("/api/glass/purchase/application")
public class PurchaseApplicationController {


    @Autowired
    private PurchaseApplicationService purchaseApplicationService;


    @PostMapping("/list")
    @ApiOperation(value = "采购申请列表", response = ResultBody.class)
    public ResultBody queryList(@RequestBody PurchaseApplicationReq purchaseApplicationReq) {
        return ResultBody.success(purchaseApplicationService.getPage(purchaseApplicationReq));
    }

    @PostMapping("/add")
    @ApiOperation(value = "采购申请列表新增", response = ResultBody.class)
    public ResultBody add(@RequestBody PurchaseApplicationList purchasePurchaseApplicationList) {
        purchaseApplicationService.addGPurchasePurchaseApplicationList(purchasePurchaseApplicationList);
        return ResultBody.success();
    }

    @PostMapping("/approveProcess")
    @ApiOperation(value = "发起申请流程", response = ResultBody.class)
    public ResultBody approveProcess(@RequestBody BpmProcessInstanceCreate BpmProcessInstanceCreate) {
        purchaseApplicationService.approveProcess(BpmProcessInstanceCreate);
        return ResultBody.success();
    }

    @PostMapping("/edit")
    @ApiOperation(value = "采购申请列表修改", response = ResultBody.class)
    public ResultBody edit(@RequestBody PurchaseApplicationList purchasePurchaseApplicationList) {
        purchaseApplicationService.updateGPurchasePurchaseApplicationList(purchasePurchaseApplicationList);
        return ResultBody.success();
    }

    @GetMapping("/getOne/{id}")
    @ApiOperation(value = "采购申请获取对象", response = ResultBody.class)
    public ResultBody getOne(@PathVariable("id") String id) {
        return ResultBody.success(purchaseApplicationService.getGPurchasePurchaseApplicationListById(id));
    }

    @GetMapping("/getById/{id}")
    @ApiOperation(value = "详情", response = ResultBody.class)
    public ResultBody getById(@PathVariable("id") String id) {
        PurchaseApplicationList gPurchasePurchaseApplicationListById = purchaseApplicationService.getGPurchasePurchaseApplicationListById(id);
        return ResultBody.success(gPurchasePurchaseApplicationListById);
    }

    @PostMapping("/special/list")
    @ApiOperation(value = "特采跟踪列表", response = ResultBody.class)
    public ResultBody specialList(@RequestBody PurchaseApplicationReq purchaseApplicationReq) {
        return ResultBody.success(purchaseApplicationService.specialList(purchaseApplicationReq));
    }

    @GetMapping("/getSequenceNumber")
    @ApiOperation(value = "获取申请编号", response = ResultBody.class)
    public ResultBody getSequenceNumber() {
        return ResultBody.success(purchaseApplicationService.getSequenceNumber());
    }


    @GetMapping("/getInfoList")
    @ApiOperation(value = "采购申请列表", response = ResultBody.class)
    public ResultBody getInfoList() {
        return ResultBody.success(purchaseApplicationService.getGPurchasePurchaseApplicationListDTOList());
    }


    @PostMapping("/addApprove")
    @ApiOperation(value = "拆分子单", response = ResultBody.class)
    public ResultBody addApprove(@RequestBody PurchaseApprovalList purchaseApproval) {
        purchaseApplicationService.addApprove(purchaseApproval);
        return ResultBody.success();
    }

    @PostMapping("/importItemData")
    @ApiOperation(value = "物品数据导入", response = ResultBody.class)
    public ResultBody<Map<String, List<RequestItemImportData>>> importItemData(@RequestPart("file") MultipartFile file) {
        return ResultBody.success(purchaseApplicationService.importItemData(file));
    }

    @GetMapping("/getFileTemplate")
    @ApiOperation(value = "获取物品导入模板", response = ResultBody.class)
    public void getFileTemplate(HttpServletResponse response) {
        purchaseApplicationService.getFileTemplate(response);
    }

    @PostMapping("/exportItemData")
    @ApiOperation(value = "物品数据导出", response = ResultBody.class)
    public void exportItemData(@RequestBody List<RequestItemImportData> requestItemImportData, HttpServletResponse response) {
        purchaseApplicationService.exportItemData(requestItemImportData, response);
    }

    @PostMapping("/exportFrameItemData")
    @ApiOperation(value = "框架物品数据导出", response = ResultBody.class)
    public void exportFrameItemData(@RequestBody List<FrameItemData> frameItemData, HttpServletResponse response) {
        purchaseApplicationService.exportFrameItemData(frameItemData, response);
    }

    @PostMapping("/exportOrderItemData")
    @ApiOperation(value = "订单物品数据导出", response = ResultBody.class)
    public void exportOrderItemData(@RequestBody List<OrderItemData> orderItemData, HttpServletResponse response) {
        purchaseApplicationService.exportOrderItemData(orderItemData, response);
    }

}
